package com.wf.controller;

import cn.hutool.core.util.IdUtil;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.wf.properties.AliPayProperties;
import com.wf.util.AliPayUtil;
import com.wf.web.base.BaseController;
import com.wf.web.domain.Result;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 充值模块
 *
 * @author mpk
 * @since 2024/10/11 08:59:39
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController extends BaseController {


    @Autowired
    AliPayUtil aliPayUtil;
    @Autowired
    AliPayProperties aliPayProperties;

    @PostMapping("/recharges/{type}/{money}")
    public Result recharges(@PathVariable String type,@PathVariable String money) {

        switch (type) {
            case "alipay":
                AlipayTradeWapPayModel model = getAlipayTradeWapPayModel(money);

                aliPayUtil.appPay(model);
                return success("支付宝充值");
            case "bank":
                return success("银行卡充值");
            case "wechat":
                return success("微信充值");
            default:
                return failure("充值方式不存在");
        }
    }

    @NotNull
    private AlipayTradeWapPayModel getAlipayTradeWapPayModel(String money) {
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        // 设置商户订单号
        model.setOutTradeNo(IdUtil.getSnowflakeNextIdStr());

        // 设置支付主题
        model.setSubject("充值");

        model.setTotalAmount(money);
        // 设置支付产品代码
        model.setProductCode(aliPayProperties.getProductCode());
        // 设置卖家ID
        model.setSellerId(aliPayProperties.getSellerId());
        return model;
    }

    /**
     * 支付宝回调
     *
     * @param request
     * @return
     */
    @PostMapping("/alipay/notify")
    public Result alipayNotify(HttpServletRequest request) {

        Map<String, String> map = aliPayUtil.notifyServer(request);
        map.keySet()
                .forEach(
                        key -> log.info("key:{},value:{}", key, map.get(key))
                );

        return success();
    }

    /**
     * 银联回调
     *
     * @param request
     * @return
     */
    @PostMapping("/bank/notify")
    public Result bankNotify(HttpServletRequest request) {

        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println(parameterMap);
        return success();
    }

    /**
     * 微信回调
     *
     * @param request
     * @return
     */
    @PostMapping("/wechat/notify")
    public Result wechatNotify(HttpServletRequest request) {

        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println(parameterMap);
        return success();
    }

}
